<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Table extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->library('tank_auth');
		$this->load->model('SysTableColumns');
	}

	function index()
	{	// Перекидываем на начальную страницу, если в url не указана ф-ия, т.е. написано ...index.php/Table
		redirect('/welcome');
	}

	function View($TableName)
	{
		if (!$this->tank_auth->is_logged_in()) {
			redirect('/auth/login/');
			return;
		}
		
		$this->SysTableColumns->GetTableColumns($TableName);
		if ($TableName=='Zadach') {
			$this->load->model('Zadach');
			$Query = $this->Zadach->GetAll($this->SysTableColumns->SelectList);
		} else {
			$this->db->select($this->SysTableColumns->SelectList);
			$Query = $this->db->get($TableName);
		}
		
		$Params['TableName'] = $TableName;
		$Params['Columns'] = $this->SysTableColumns->Columns;
		$Params['PKFieldName'] = $this->SysTableColumns->PKFieldName;
		if ($Query->num_rows>0) {
			$Params['Records'] = $Query->Result();
		}
		$Params['PageTitle'] = 'Выборка - '.$this->SysTableColumns->TableCaption;
		$Params['MainContent'] = 'TableView';
		$this->load->view('TemplateView',$Params);
	}

	function Test() {
		$Params['PageTitle'] = 'Тест';
		$Params['MainContent'] = 'TreeView';
		$this->load->view('TemplateView',$Params);
	}
	
	function Edit($TableName,$KeyID)
	{
		if (!$this->tank_auth->is_logged_in()) {
			redirect('/auth/login/');
			return;
		}
		$this->SysTableColumns->GetTableColumns($TableName);
		// получаем запись из БД
		$this->db->select($this->SysTableColumns->SelectList);
		$this->db->where($this->SysTableColumns->PKFieldName,$KeyID);
		$Query = $this->db->get($TableName);
		// загружаем форму редактирования
		$Params['TableName'] = $TableName;
		$Params['Columns'] = $this->SysTableColumns->Columns;
		$Params['PKFieldName'] = $this->SysTableColumns->PKFieldName;
		$Params['PKFieldValue'] = $KeyID;
		if ($Query->num_rows()==1) {
			$Params['Records'] = $Query->Result();
		}
		$Params['PageTitle'] = 'Редактирование документа - '.$this->SysTableColumns->TableCaption;
		$Params['MainContent'] = 'TableEditView';
		$this->load->view('TemplateView',$Params);
	}
	
	private function GetTreeNodes($TableName,$ParentID) {
		$this->db->select($this->SysTableColumns->SelectList);
		$this->db->where($this->SysTableColumns->ParentIDFieldName,$ParentID);
		if (strtolower($TableName)=='zadach') {$this->db->where('DatVip IS NULL');}
		return $this->db->get($TableName);
	}
	
	private function LoadTreeNodesView($TableName,$TreeNodes,$Level) {
		if ($TreeNodes->num_rows()>0) {
			$Rows = $TreeNodes->Result_array();
			$KeyFirst = key($Rows);
			end($Rows);
			$KeyLast = key($Rows);
			foreach ($Rows as $RecordNumber => $Node) {
				$NodeSiblings = $this->GetTreeNodes($TableName,$Node[$this->SysTableColumns->PKFieldName]);
				//$Params['TreeNode'] = $Node;
				$Params['IsFirst'] = ($RecordNumber==$KeyFirst);
				$Params['IsLast'] = ($RecordNumber==$KeyLast);
				$Params['IsRoot'] = ($Node[$this->SysTableColumns->ParentIDFieldName]==Null);
				$Params['IsLeaf'] = ($NodeSiblings->num_rows()==0);
				$Params['Level'] = $Level;
				$Params['LeftPadding'] = str_repeat("\t",$Level);
				$Params['TableName'] = $TableName;
				$Params['PKFieldValue'] = $Node[$this->SysTableColumns->PKFieldName];
				$Params['NodeText'] = $Node[$this->SysTableColumns->AnchorFieldName];
				$this->load->view('TreeNodeView',$Params);
				$this->LoadTreeNodesView($TableName,$NodeSiblings,$Level+1);
			}
			$this->load->view('TreeNodeCloseView');
		}
	}
	
	function ViewTree($TableName)
	{
		if (!$this->tank_auth->is_logged_in()) {
			redirect('/auth/login/');
			return;
		}
		$this->SysTableColumns->GetTableColumns($TableName);
		
		$Params['PageTitle'] = 'Дерево - '.$this->SysTableColumns->TableCaption;
		$Params['PKFieldName'] = $this->SysTableColumns->PKFieldName;
		$this->load->view('TemplateHeader',$Params);
		$this->load->view('TreeViewHeader');
		if ($this->SysTableColumns->ParentIDFieldName<>'') {
			$this->LoadTreeNodesView($TableName,$this->GetTreeNodes($TableName,NULL),0);
		}
		$this->load->view('TreeViewFooter');
		$this->load->view('TemplateFooter');
	}
	
	function Save($TableName,$KeyID) {
		$this->SysTableColumns->GetTableColumns($TableName);
		$this->load->model('GeneralDocs');
		$this->GeneralDocs->Save($TableName,$KeyID);
	}
}

